package com.learner.course.service.impl;

import com.learner.course.mapper.CourseCollectMapper;
import com.learner.course.repository.CourseCollectRepository;
import com.learner.course.service.CourseCollectService;
import com.learner.pojo.course.CourseCollect;
import com.learner.pojo.course.expand.CourseCollectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/3/8
 * @Description: 课程收藏
 **/
@Slf4j
@Service
public class CourseCollectServiceImpl implements CourseCollectService {

    @Autowired
    private CourseCollectRepository courseCollectRepository;

    @Autowired
    private CourseCollectMapper courseCollectMapper;

    /**
     * @Description: 收藏表添加一条记录
     * @Author: ice2020x
     * @Date: 2021/10/28
     */
    @Override
    public void save(String courseId, String id) {
        if (!this.isCollect(courseId, id)) {
            CourseCollect courseCollect = new CourseCollect();
            courseCollect.setCourseId(courseId);
            courseCollect.setMemberId(id);
            courseCollectRepository.save(courseCollect);
        }
    }

    @Override
    public boolean isCollect(String courseId, String id) {
        CourseCollect courseCollect = courseCollectRepository.findByCourseIdAndMemberId(courseId, id);
        return courseCollect != null;
    }

    @Override
    public int removeCourseCollect(String courseId, String userId) {
        return courseCollectRepository.deleteByCourseIdAndMemberId(courseId, userId);
    }

    @Override
    public List<CourseCollectVo> getCourseCollectList(String userId) {
        log.info("获取课程收藏列表,当前登录id：{}",userId);
        return courseCollectMapper.selectPageByMemberId(userId);
    }
}
